package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "用户模块")
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "用户登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("/login")
    public JsonResult login(@Validated @RequestBody UserLoginParam userLoginParam) {
        log.debug("用户登录:UserLoginParam+{}"+ userLoginParam);
        //鼠标点击方法名，配合快捷键:Ctrl去接口，Ctrl+Alt+M去实现类
        UserVO  userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @Operation(summary = "用户查询")
    @ApiOperationSupport(order = 20)
    @GetMapping("/select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("用户查询:UserQuery+{}", userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @Operation(summary = "保存用户")
    @ApiOperationSupport(order = 30)
    @PostMapping("/save")
    public JsonResult saveUser(@Validated UserSaveParam userSaveParam){
        log.debug("保存用户:UserSaveParam+{}", userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @Operation(summary = "重置密码")
    @ApiOperationSupport(order = 40)
    @PostMapping("update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码:userId={}", userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @Operation(summary = "修改状态")
    @ApiOperationSupport(order = 50)
    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId, @PathVariable String status){
        log.debug("修改用户状态:userId={},status={}", userId, status);
        userService.updateStatus(userId, status);
        return JsonResult.ok();
    }

    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 60)
    @PostMapping("delete/{userId}")
    public JsonResult delete(@PathVariable Long userId){
        log.debug("删除员工:userId={}", userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @Operation(summary = "查询审批人")
    @ApiOperationSupport(order = 70)
    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId){
        log.debug("查询审批人:parentId={}", parentId);
        List<UserVO> list = userService.selectAuditUserList(parentId);
        return JsonResult.ok(list);
    }
}
